<template>
	<view>
		汇总记录：<br/>
		<textarea v-model="oldData" maxlength="100000">
			
		</textarea>
		处理后:<br/>
		<textarea v-model="newData" maxlength="100000">
			
		</textarea>
		<button @click="deal">
			处理
		</button>
		<button @click="dealDev">
			处理设备
		</button>
		后工序设备列表：<br/>
		<textarea v-model="devListData" maxlength="100000">
			
		</textarea>
		<button @click="undo">
			未做列表
		</button>
	</view>
</template>

<script>
	import axios from 'axios';
	import tableData from './tableData.js'
	import * as XLSX from 'xlsx';
	import {
		saveAs
	} from 'file-saver';
	export default {
		data() {
			return {
				oldData:'',
				newData:'',
				devListData:'',//设备列表
				peopleArrOld:[],
				peopleArrNew:[],
				peopleArr:[
					'李钦章',
					'陈锡漫',
					'陈志威',
					'董梓浩',
					'梁伟均',
					'李波源',
					'黎富恒',
					'李金良',
					'黎伟锋',
					'李晓通',
					'罗星威',
					'马明锋',
					'莫本慰',
					'莫志杰',
					'覃基发',
					'吴烨希',
					'吴永健',
					'肖光',
					'谢颖通',
					'赵敏羽',
					'赵伟锋',
					'郑军平',
					'朱楷城',
					'邹仲强'
				],
				deviceArrOld:[],
				deviceArrNew:[],
				deviceArr:[
				],
				textContent:''
			}
		},
		onLoad() {
			
		},
		created() {
			
		},
		computed: {
	
		},
		methods: {
			deal(){
				
				var Arr1=this.oldData.split('\n');
				this.peopleArrOld=[];
				for (var i = 0; i < Arr1.length; i++) {
					var item=Arr1[i];
					var itemArr=item.split('\t');
					if(itemArr[0]!=''){
						this.peopleArrOld.push({
							'name':itemArr[0],
							'count':itemArr[1]
						})
					}
					
				}
				//初始化
				for (var i = 0; i < this.peopleArr.length; i++) {
					var item=this.peopleArr[i];
					this.peopleArrNew.push({
						'name':item,
						'count':0
					})
				}
				//赋值
				for (var i = 0; i < this.peopleArrNew.length; i++) {
					var item=this.peopleArrNew[i];
					for (var j = 0; j < this.peopleArrOld.length; j++) {
						var jitem=this.peopleArrOld[j];
						if(jitem.name==item.name){
							this.peopleArrNew[i]=jitem;
						}
					}
				}
				console.log(this.peopleArrNew);
				this.newData='';
				for (var i = 0; i < this.peopleArrNew.length; i++) {
				   var item=this.peopleArrNew[i];
				   this.newData+=item.name+'\t'+item.count+'\n';
				}
			},
			dealDev(){
				//设备
				this.appendDev();
				
				var Arr1=this.oldData.split('\n');
				this.deviceArrOld=[];
				for (var i = 0; i < Arr1.length; i++) {
					var item=Arr1[i];
					var itemArr=item.split('\t');
					if(itemArr[0]!=''){
						this.deviceArrOld.push({
							'name':itemArr[0],
							'count':itemArr[1]
						})
					}
					
				}
				//初始化
				for (var i = 0; i < this.deviceArr.length; i++) {
					var item=this.deviceArr[i];
					this.deviceArrNew.push({
						'name':item.name,
						'count':0
					})
				}
				
				//赋值
				for (var i = 0; i < this.deviceArrNew.length; i++) {
					var item=this.deviceArrNew[i];
					console.log(item.name,item.count)
					if(item.name=='DG-2024-0010')
					{
						console.log(1111);
					}
					for (var j = 0; j < this.deviceArrOld.length; j++) {
						var jitem=this.deviceArrOld[j];
						
						if(jitem.name.indexOf(item.name)>-1){
							this.deviceArrNew[i]=jitem;
							console.log(jitem);
						}
					}
				}
				console.log(this.deviceArrNew);
				this.newData='';
				for (var i = 0; i < this.deviceArrNew.length; i++) {
				   var item=this.deviceArrNew[i];
				   this.newData+=item.name+'\t'+item.count+'\n';
				}
			},
			//未完成情况
			undo(){
				//设备
				this.appendDev();
				
				var Arr1=this.oldData.split('\n');
				this.deviceArrOld=[];
				for (var i = 0; i < Arr1.length; i++) {
					var item=Arr1[i];
					var itemArr=item.split('\t');
					if(itemArr[0]!=''){
						this.deviceArrOld.push({
							'name':itemArr[0],
							'count':itemArr[1]
						})
					}
					
				}
				//初始化
				for (var i = 0; i < this.deviceArr.length; i++) {
					var item=this.deviceArr[i];
					this.deviceArrNew.push({
						'name':item.name,
						'count':0
					})
				}
				
				//赋值
				for (var i = 0; i < this.deviceArrNew.length; i++) {
					var item=this.deviceArrNew[i];
					console.log(item.name,item.count)
					for (var j = 0; j < this.deviceArrOld.length; j++) {
						var jitem=this.deviceArrOld[j];
						
						if(jitem.name.indexOf(item.name)>-1){
							this.deviceArrNew[i]=jitem;
							console.log(jitem);
						}
					}
				}
				console.log(this.deviceArrNew);
				var ArrList=this.devListData.split('\n');//设备列表
				this.newData='';
				for (var i = 0; i < ArrList.length; i++) {
					this.newData+=this.matchCount(this.deviceArrNew,ArrList[i])+'\n';
				}
				
			},
			matchCount(devCountArr,GDZC){
				var count=0;
				for (var i = 0; i < devCountArr.length; i++) {
					var item=devCountArr[i];
					//是同一个设备资产
					if(item.name.indexOf(GDZC)>-1){
						count=item.count;
						console.log(item,GDZC);
						break;
					}
				}
				return count;
			},
			appendDev() {
				if (this.deviceArr.length < 100) {
					this.loadTextFile('../../device.txt'); //加载人员数组
				}
			},
			loadTextFile(filePath) {
				fetch(filePath)
					.then(response => response.text())
					.then(text => {
						this.textContent = text;
						this.getarr();
					})
					.catch(error => {
						console.error('Error reading file:', error);
					});
			},
			getarr() {
				var arr = this.textContent.split("\n")
			
				for (var i = 0; i < arr.length; i++) {
					var line = arr[i];
					this.deviceArr.push({
						'name':line,
						'count':0
					})
				}
				console.log(this.deviceArr);
			},
			//导出excel
			exportToExcel() {
				// 假设你有一个表格数据的数组
			
				var filename = "故障代码";
				 var tempArr = this.groupData;
				var data = [
					["故障代码", "描述", "报警等级","异常处理方式"],
				];
				for (var i = 0; i < tempArr.length; i++) {
					data.push([tempArr[i].name, tempArr[i].description, tempArr[i].level,tempArr[i].deal]);
				}
			
				// 将数据转换为工作表
				const worksheet = XLSX.utils.aoa_to_sheet(data);
			
				// 创建工作簿并添加工作表
				const workbook = XLSX.utils.book_new();
				XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
			
				// 生成Excel文件
				const excelBuffer = XLSX.write(workbook, {
					bookType: 'xlsx',
					type: 'array'
				});
			
				// 使用blob和FileReader创建一个Blob URL
				const dataBlob = new Blob([excelBuffer], {
					type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
				});
				const blobUrl = window.URL.createObjectURL(dataBlob);
			
				// 使用saveAs下载文件
				saveAs(dataBlob,  filename + 'export' + (new Date().toLocaleDateString()) + '.xlsx');
			
				// 清理
				window.URL.revokeObjectURL(blobUrl);
			},
		}
	}
</script>

<style>
</style>